home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / archival / genindex / index-to-ls < prev    next >
Encoding:
Text File  |  1992-08-27  |  4.1 KB  |  186 lines

  1. #!/bin/sh
  2. # $Id: index-to-ls,v 1.2 1992/08/11 16:31:33 cross Exp $
  3.  
  4. TZ=GMT
  5. export TZ
  6. unset LANG
  7. unset LANGUAGE
  8. unset LC_CTYPE
  9.  
  10. if [ $# -ne 1 -o ! -r "$1" ]; then
  11.     echo "Usage: $0 index > ls-lR" 1>&2
  12.     exit 1
  13. fi
  14. date '+%d %m %y' | cat - "$1" | awk '
  15. NR == 1 {
  16.     thisday=$1; thismonth=$2; thisyear=$3; 
  17.     if (thisyear > 1900)
  18.     thisyear -= 1900;
  19.     today=thisday + 30 * thismonth + 365 * thisyear;
  20.     months[1]  = "Jan"; months[2]  = "Feb"; months[3]  = "Mar";
  21.     months[4]  = "Apr"; months[5]  = "May"; months[6]  = "Jun";
  22.     months[7]  = "Jul"; months[8]  = "Aug"; months[9]  = "Sep";
  23.     months[10] = "Oct"; months[11] = "Nov"; months[12] = "Dec";
  24.     next;
  25. }
  26. /^#VERSION/ {
  27.     if ($2 != "1.0") {
  28.     print "wrong version header \"" $2 "\". cannot decode format.";
  29.     err=1;
  30.     exit;
  31.     }
  32.     version=1;
  33.     next;
  34. }
  35. /^$/ || /^#/ {
  36.     next;
  37. }
  38. NF < 5 {
  39.     print "not enough fields in line " NR - 1 ;
  40.     err=1;
  41.     exit;
  42. }
  43. {            # COMPOSE (print see below)
  44.     if (!version) {
  45.     print "no version (#VERSION) header provided. cannot decode format.";
  46.     err=1;
  47.     exit;
  48.     }
  49.     type=substr($1,1,1);
  50.     if (type == "F")
  51.     type="-";
  52.     else if (type == "D")
  53.     type="d";
  54.     else if (type == "L")
  55.     type="l";
  56.     else {
  57.     print "illegal type specification in line " NR - 1 ;
  58.     err=1;
  59.     exit;
  60.     }
  61.  
  62.     if (substr($1,2,1) == "R")
  63.     perm="r";
  64.     else
  65.     perm="-";
  66.     if (substr($1,3,1) == "W")
  67.     perm= perm "w";
  68.     else
  69.     perm= perm "-";
  70.     if (substr($1,4,1) == "X")
  71.     perm= perm "x";
  72.     else
  73.     perm= perm "-";
  74.     perm= perm perm perm;
  75.  
  76.     day=substr($3,1,2); monthstr=substr($3,4,3); year=substr($3,8,11) - 1900;
  77.     time=$4;
  78.  
  79.     for (month = 12; month > 0; month--) {
  80.     if (months[month] == monthstr) break;
  81.     }
  82.  
  83.     if (month <= 0) {
  84.     print "illegal month specification in line " NR - 1 ;
  85.     err=1;
  86.     exit;
  87.     }
  88.  
  89.     for(i = length($5); i >= 0 && substr($5,i,1) != "/"; i--)
  90.     ;
  91.     if (i < 0) {
  92.     print "filename does not begin with / in line " NR - 1 ;
  93.     err=1;
  94.     exit;
  95.     }
  96.  
  97.     # must be seeked from left because of embedded spaces in filenames!
  98.     # no offset range check; tested earlier (NF >= 5)
  99.     foff=0;
  100.     for(i = 0; i < 4; i++) {
  101.     while(substr($0,foff,1) != " " && substr($0,foff,1) != "    ")
  102.         foff++;
  103.     while(substr($0,foff,1) == " " || substr($0,foff,1) == "    ")
  104.         foff++;
  105.     }
  106.     if (substr($0,foff,1) == "/")
  107.     foff++;
  108.     if (type == "l") {
  109.     offl=foff;
  110.     len=length($0);
  111.     while(offl < len - 4 && substr($0,offl,4) != " -> ")
  112.         offl++;
  113.     if (offl >= len - 4) {
  114.         print "illegal link name syntax in line " NR - 1;
  115.         err=1;
  116.         exit;
  117.     }
  118.     path=substr($0,foff,offl-foff);
  119.     link=substr($0,offl+4,1024);
  120.     } else {
  121.     path=substr($0,foff,1024);
  122.     }
  123.     olddepth = depth;
  124.     depth=0;
  125.     len=length(path);
  126.     fileoff=0;
  127.     for(off=0; off < len; off++) {
  128.     if (substr(path,off,1) == "/") {
  129.         depth++;
  130.         fileoff=off+1;
  131.     }
  132.     }
  133.     file=substr(path,fileoff,1024);
  134.  
  135.     line=sprintf("%s%s  1 dummy    %8d ", type, perm, $2);
  136.     if (day + 30 * month + 365 * year + 6 * 30 > today) {
  137.     line=line sprintf("%s %2d %s", monthstr, day, time);
  138.     } else {
  139.     line=line sprintf("%s %2d %5d", monthstr, day, year + 1900);
  140.     }
  141.     line=line " " file;
  142.     if (type == "l") {
  143.     line=line " -> " link;
  144.     }
  145.     line=line "\n";
  146.  
  147.     # DIVERT or PRINT composed line
  148.  
  149.     if (olddepth > depth) {
  150.     for(d = olddepth - 1; d >= depth; d--) {
  151.         subdirs[d] = subdirs[d] "\n" name[d+1] ":\ntotal " count[d+1] "\n" divert[d+1] subdirs[d+1];
  152.         divert[d+1] = "";
  153.         subdirs[d+1] = "";
  154.         name[d+1] = "";
  155.         count[d+1] = 0;
  156.     }
  157.     }
  158.     if (type == "d") {
  159.     divert[depth] = divert[depth] line;
  160.     count[depth]++;
  161.     depth++;    # remember that we began a new directory
  162.     name[depth] = file; 
  163.     count[depth] = 0;
  164.     subdirs[depth] = "";
  165.     next;
  166.     } 
  167.     if (olddepth < depth ) {
  168.     print "directory name for new directory missing in line " NR - 1;
  169.     err=1;
  170.     exit;
  171.     }
  172.     
  173.     divert[depth] = divert[depth] line;
  174.     count[depth]++;
  175. }
  176. END {
  177.     if (err) exit;
  178.     for(; depth >= 0; depth--) {
  179.     if (name[depth+1] != "") {
  180.         subdirs[depth] = subdirs[depth] "\n" name[depth+1] ":\ntotal " count[depth+1] "\n" divert[depth+1] subdirs[depth+1];
  181.     }
  182.     }
  183.     print "total " count[0] "\n" divert[0] subdirs[0];
  184. }
  185. '
  186.